package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xca.znb.R;
import com.xca.znb.adapter.MsgAdapter;
import com.xca.znb.entity.Message;
import com.xca.znb.entity.Msg;
import com.xca.znb.entity.User;
import com.xca.znb.model.impl.MessageImpl;
import com.xca.znb.presenter.MessagePresenter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends AppCompatActivity {
    private List<Msg> mMsgList = new ArrayList<>();

    private EditText inputText;

    private RecyclerView mRecyclerView;

    private MsgAdapter mAdapter;
    private ImageButton backBtn ;
    private EditText mInput ;

    private MessagePresenter presenter ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_list);
        backBtn = findViewById(R.id.message_back_btn);
        mRecyclerView = findViewById(R.id.message_recycler_view);
        inputText = findViewById(R.id.message_input_text);

        MessageImpl messageImpl = new MessageImpl(this);
        presenter = new MessagePresenter(messageImpl);

        initMsg();

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new MsgAdapter(mMsgList);
        mRecyclerView.setAdapter(mAdapter);

        // 将RecyclerView定位到最后一行
        mRecyclerView.scrollToPosition(mMsgList.size() - 1);

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }


    private void initMsg(){
        //登录电话作为发送者id
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");
        //接收传递过来的接收者ID
        Intent intent = getIntent();
        String receiver = intent.getStringExtra("goods_receive");
        User user1 = presenter.getUserByPhone(phone);
        User user2 = presenter.getUserByPhone(receiver);

        List<Message> messages = presenter.getMessage();
        for (Message message : messages){
            if (message.getSend_id().equals(phone)&&message.getReceive_id().equals(receiver)){
                mMsgList.add(new Msg(message.getText() , Msg.TYPE_SENT , message.getTime(),user1.getConcern()));
            } else if (message.getSend_id().equals(receiver)&& message.getReceive_id().equals(phone)) {
                mMsgList.add(new Msg(message.getText() , Msg.TYPE_RECEIVED , message.getTime() , user2.getConcern()));
            }
        }
    }

    public void sendMsg(View view) {
        LocalDateTime nowTime = LocalDateTime.now();
        String secTime = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        String content = inputText.getText().toString() ;
        //登录电话作为发送者id
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");
        //接收传递过来的接收者ID
        Intent intent = getIntent();
        String receiver = intent.getStringExtra("goods_receive");
        User user1 = presenter.getUserByPhone(phone);

        Message message = new Message(phone,receiver,content , secTime);

        if(!"".equals(content)) {
            Msg msg = new Msg(content,Msg.TYPE_SENT , secTime,user1.getConcern());
            presenter.sendMessage(phone,receiver,content,secTime);
            mMsgList.add(msg);

            //有新消息时，刷新RecyclerView中的显示
            mAdapter.notifyItemInserted(mMsgList.size() - 1);

            //将RecyclerView定位到最后一行
            mRecyclerView.scrollToPosition(mMsgList.size() - 1);

            //清空输入框中的内容
            inputText.setText("");

        }
    }

//    private void initMsg() {
//        SharedPreferences sharedPreferences = getSharedPreferences("loginMsg" , Context.MODE_PRIVATE) ;
//        String userId = sharedPreferences.getString("userId" , "");
//        Intent intent = getIntent();
//        String catchId = intent.getStringExtra("catchId");
//
//        List<Message> messages = msgDBHelper.getAllMsg();
//
//        for (Message message : messages){
//            if (message.getSendId().equals(userId)&&message.getCatchId().equals(catchId)){
//                mMsgList.add(new Msg(message.getMsgText() , Msg.TYPE_SENT));
//            } else if (message.getSendId().equals(catchId)&& message.getCatchId().equals(userId)) {
//                mMsgList.add(new Msg(message.getMsgText() , Msg.TYPE_RECEIVED));
//            }
//        }
//    }
//
//    public void sendMsg(View view) {
//        String content = inputText.getText().toString();
//        SharedPreferences sharedPreferences = getSharedPreferences("loginMsg" , Context.MODE_PRIVATE) ;
//        String userId = sharedPreferences.getString("userId" , "");
//        Intent intent = getIntent();
//        String catchId = intent.getStringExtra("catchId");
//        Message message = new Message(userId , catchId ,content);
//
//        if(!"".equals(content)) {
//            Msg msg = new Msg(content,Msg.TYPE_SENT);
//            msgDBHelper.insertMessage(message);
//            mMsgList.add(msg);
//
//            //有新消息时，刷新RecyclerView中的显示
//            mAdapter.notifyItemInserted(mMsgList.size() - 1);
//
//            //将RecyclerView定位到最后一行
//            mRecyclerView.scrollToPosition(mMsgList.size() - 1);
//
//            //清空输入框中的内容
//            inputText.setText("");
//
//        }
//    }
}
